package com.hl.ruoyilkshop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.ruoyilkshop.domain.GoodsSpecRelation;
import com.hl.ruoyilkshop.service.IGoodsSpecRelationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品规格关联Controller
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/relation1")
public class GoodsSpecRelationController extends BaseController
{
    @Autowired
    private IGoodsSpecRelationService goodsSpecRelationService;

    /**
     * 查询商品规格关联列表
     */
    @RequiresPermissions("lkshop:relation1:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodsSpecRelation goodsSpecRelation)
    {
        startPage();
        List<GoodsSpecRelation> list = goodsSpecRelationService.selectGoodsSpecRelationList(goodsSpecRelation);
        return getDataTable(list);
    }

    /**
     * 导出商品规格关联列表
     */
    @RequiresPermissions("lkshop:relation1:export")
    @Log(title = "商品规格关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsSpecRelation goodsSpecRelation)
    {
        List<GoodsSpecRelation> list = goodsSpecRelationService.selectGoodsSpecRelationList(goodsSpecRelation);
        ExcelUtil<GoodsSpecRelation> util = new ExcelUtil<GoodsSpecRelation>(GoodsSpecRelation.class);
        util.exportExcel(response, list, "商品规格关联数据");
    }

    /**
     * 获取商品规格关联详细信息
     */
    @RequiresPermissions("lkshop:relation1:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(goodsSpecRelationService.selectGoodsSpecRelationById(id));
    }

    /**
     * 获取商品规格关联详细信息（包含规格值）
     */
    @RequiresPermissions("lkshop:relation1:query")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        return success(goodsSpecRelationService.selectGoodsSpecRelationDetail(id));
    }

    /**
     * 获取商品规格关联详细信息（从SKU表获取商品对应的具体规格值）
     */
    @RequiresPermissions("lkshop:relation1:query")
    @GetMapping(value = "/detailWithSku/{id}")
    public AjaxResult getDetailWithSku(@PathVariable("id") Long id)
    {
        return success(goodsSpecRelationService.selectGoodsSpecRelationDetailWithSku(id));
    }

    /**
     * 从SKU表获取商品对应的具体规格值
     */
    @RequiresPermissions("lkshop:relation1:query")
    @GetMapping(value = "/specsFromSku/{id}")
    public AjaxResult getSpecsFromSku(@PathVariable("id") Long id)
    {
        return success(goodsSpecRelationService.selectSpecsFromSku(id));
    }

    /**
     * 新增商品规格关联
     */
    @RequiresPermissions("lkshop:relation1:add")
    @Log(title = "商品规格关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsSpecRelation goodsSpecRelation)
    {
        return toAjax(goodsSpecRelationService.insertGoodsSpecRelation(goodsSpecRelation));
    }

    /**
     * 修改商品规格关联
     */
    @RequiresPermissions("lkshop:relation1:edit")
    @Log(title = "商品规格关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsSpecRelation goodsSpecRelation)
    {
        return toAjax(goodsSpecRelationService.updateGoodsSpecRelation(goodsSpecRelation));
    }

    /**
     * 删除商品规格关联
     */
    @RequiresPermissions("lkshop:relation1:remove")
    @Log(title = "商品规格关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(goodsSpecRelationService.deleteGoodsSpecRelationByIds(ids));
    }
}
